<?php
/**
 * @return
 * @author 朱乐斌
 * @date
 */

namespace app\api\controller;

use app\api\model\DeliverNotesModel;
use app\api\model\EducationModel;
use app\api\model\ResumeModel;
use app\api\model\UserGxModel;
use app\api\model\WorkModel;
use app\api\validate\EditNotesValidate;
use app\api\validate\NotesValidate;
use think\Exception;
use think\Request;

class Notes extends BaseController
{

    // 管理查看我的简历
    public function manageNotes(Request $request)
    {
        try {
            // 获取用户信息
            $user = UserGxModel::where('phone',$request->phone)->field('id,type')->find();
            if($user['type'] == 2) return errMsg('您是企业用户，无需管理');
            $resume = ResumeModel::where('user_id',$user['id'])->withoutField('create_time,update_time,delete_time')->find()->toArray();
            $resume['sex'] = $resume['sex'] == 1 ? '男' : '女';
            return succMsg($resume);
        }catch (\Exception $e){
            return errMsg($e->getMessage());
        }
    }


    // 添加简历
    public function addNotes(Request $request)
    {
        // 接收添加简历的值
        $data = $this->request->post();
        try {
            $user = UserGxModel::where('phone',$request->phone)->field('id,type')->find();
            if ($user['type'] == 2) return errMsg('您不是企业用户，不能添加简历');
            // 验证数据的准确性
            validate(NotesValidate::class)->scene('addNotes')->check($data);
            // 查询是否已存在简历
            $resume = ResumeModel::where('user_id',$user['id'])->find();
            if (!empty($resume)) return errMsg('无需再添加简历');
            $resumeDate = [
                'user_id' => $user['id'],
                'name' => $data['name'],
                'sex' => $data['sex'],
                'phone' => $data['phone'],
                'id_card' => $data['id_card'],
                'email' => $data['email'],
                'image' => $data['image'],
                'address' =>$data['address'],
                'qualifications_id' => $data['qualifications_id'],
                'position_id' => $data['position_id'],
                'salary' => $data['salary'],
                'expertise' => $data['expertise'],
                'content' => $data['content'],
                'job_id' => $data['job_id'],
                'state' => $data['state']
            ];
            $resume = new ResumeModel();
            $resume = $resume->save($resumeDate);
            if ($resume) return errMsg('保存成功');
        }catch (\Exception $e){
            return errMsg($e->getMessage());
        }
        return 0;
    }

    // 修改简历
    public function editNotes(Request $request)
    {
        // 接收要修改的数据
        $data = $this->request->post();
        try {
            // 获取用户id查询用户的简历
            $user = UserGxModel::where('phone',$request->phone)->field('id,type')->find();
            if ($user['type'] == 2) return errMsg('你是企业用户要啥简历');
            // 查出用户简历
            $resume = ResumeModel::where('user_id','=',$user['id'])->find();
            // 验证接收的信息如果有其中一个就修改有的，没有就忽略
            $keys = ['name','phone','id_card','sex','address','content','email','image','state'];
            foreach ($keys as $key){
                if (isset($data[$key])) {
                    $resume->$key = $data[$key];
                }
            }
            // 验证数据是否正确
            validate(EditNotesValidate::class)->scene('editNotes')->check($data);
            $resume->save($data);
            return errMsg('修改成功');
        }catch (\Exception $e){
            return errMsg($e->getMessage());
        }
    }

    // 删除简历
    public function delNotes(Request $request)
    {
        // 接收要删除的id
        $data = $this->request->post();
        try {
            // 获取用户信息
            $user = UserGxModel::where('phone',$request->phone)->field('id,type')->find();
            // 查询用户的简历
            $resume =  ResumeModel::where('user_id','=',$user['id'])->find();
            // 删除简历
            if ($resume['user_id'] === $user['id']){
                // 删除工作经历
                $work = WorkModel::where('user_id',$resume['id'])->find();
                if (!empty($work)){
                    $work->delete();
                    // 删除教育经历
                    $education = EducationModel::where('resume_id',$resume['id'])->find();
                    if (!empty($education)) {
                        $education->delete();
                        // 删除投递简历
                        $deliverNotes = DeliverNotesModel::where('user_id', $user['id'])->find();
                        if (!empty($deliverNotes)) $deliverNotes->delete();
                    }
                }
                $resume->delete();
                $this->success('删除成功');
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

}